package com.juc.concurrency.jucExample.atomic;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author huangweiyue
 * @description AtomicLong
 * @date Created in 2021-08-01
 */
public class AtomicLongExample7 {
    private static AtomicLong atomicLong = new AtomicLong();
    private static Integer[] arrayOne = new Integer[]{0, 1, 2, 3, 0, 5, 6, 0, 56, 0};
    private static Integer[] arrayTwo = new Integer[]{10, 1, 0, 3, 0, 5, 6, 0, 13, 0};

    public static void main(String[] args) throws InterruptedException {
        Thread one = new Thread(() -> {
            int size = arrayOne.length;
            for (int i = 0; i < size; i++) {
                if (arrayOne[i].intValue() == 0) {
                    atomicLong.incrementAndGet();
                }
            }
        });

        Thread two = new Thread(() -> {
            int size = arrayTwo.length;
            for (int i = 0; i < size; i++) {
                if (arrayTwo[i].intValue() == 0) {
                    atomicLong.incrementAndGet();
                }
            }
        });

        one.start();
        two.start();

        //等待线程执行完毕
        one.join();
        two.join();

        System.out.println("count: " + atomicLong.get());
    }
}
